do "E:/ReplicateBuild/02_code/00_environment/00_set_environment.do"

use "$basedata/course_mem_clean.dta", clear

gen counter = 1
egen section_code = group(section)
collapse (sum) counter, by(teachid year schlcode ncerdc_lea section_code grade)
collapse (count) section_code (sum) counter, by(teachid year schlcode ncerdc_lea grade)

bys grade: tab section_code

bys year: tab section_code if grade==4
bys year: tab section_code if grade==5

bys year: tab section_code if grade==5 & ncerdc_lea=="XXX" // hide identity

gen multiple_sections = (section_code>1)

qui summ year
local mint = r(min)
local maxt = r(max)
forv y=`mint'/`maxt' {
    qui summ multiple_sections if year==`y'
	local p_`y' = r(mean)
    forv g=4/8 {
	    qui summ multiple_sections if grade==`g' & year==`y'
		local p_`g'_`y' = r(mean)
	}
	
}

gen ever_multi = section_code>1

collapse (max) ever_multi, by(teachid)

tempfile tempteachers
save `tempteachers', replace


use "$basedata/estimationdata/All_T_J/teach_pref_est_data.dta", clear

keep if estsampall==1
ren ncerdc_id teachid

keep teachid
duplicates drop

sort teachid
merge 1:1 teachid using `tempteachers'

bys _m: tab ever
qui summ ever
local sample_ever = r(mean)



gen avar = .
gen bvar = .
gen cvar = .
gen dvar = .
gen evar = .
gen fvar = .
gen gvar = .
gen hvar = .

label var avar "$2012$"
label var bvar "$2013$"
label var cvar "$2014$"
label var dvar "$2015$"
label var evar "$2016$"
label var fvar "$2017$"
label var gvar "$2018$"
label var hvar "Estimation sample"

mat a = (`p_2012',`p_4_2012',`p_5_2012',`p_6_2012',`p_7_2012',`p_8_2012')
mat b = (`p_2013',`p_4_2013',`p_5_2013',`p_6_2013',`p_7_2013',`p_8_2013')
mat c = (`p_2014',`p_4_2014',`p_5_2014',`p_6_2014',`p_7_2014',`p_8_2014')
mat d = (`p_2015',`p_4_2015',`p_5_2015',`p_6_2015',`p_7_2015',`p_8_2015')
mat e = (`p_2016',`p_4_2016',`p_5_2016',`p_6_2016',`p_7_2016',`p_8_2016')
mat f = (`p_2017',`p_4_2017',`p_5_2017',`p_6_2017',`p_7_2017',`p_8_2017')
mat g = (`p_2018',`p_4_2018',`p_5_2018',`p_6_2018',`p_7_2018',`p_8_2018')
mat h = (`sample_ever',.,.,.,.,.)

matrix rownames a = avar
matrix rownames b = bvar
matrix rownames c = cvar
matrix rownames d = dvar
matrix rownames e = evar
matrix rownames f = fvar
matrix rownames g = gvar
matrix rownames h = hvar

matrix j=a\b\c\d\e\f\g\h

frmttable using "$tables/multiple_classes", statmat(j) replace va tex fra ///
	ctitles("Year","All","Grade 4","Grade 5","Grade 6","Grade 7","Grade 8") ///
	sdec(3,3,3,3,3,3 \ 3,3,3,3,3,3 \ 3,3,3,3,3,3 \ 3,3,3,3,3,3 \ 3,3,3,3,3,3 \ 3,3,3,3,3,3 \ 3,3,3,3,3,3)
